package com.sitanyun.merchant.guide.view.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.base.BaseActivity;
import com.sitanyun.merchant.guide.base.ToastUtil;
import com.sitanyun.merchant.guide.bean.LoginBean;
import com.sitanyun.merchant.guide.bean.SmsPhoneBean;
import com.sitanyun.merchant.guide.presenter.impl.SmsLoginAPresenterImpl;
import com.sitanyun.merchant.guide.presenter.inter.ISmsLoginAPresenter;
import com.sitanyun.merchant.guide.view.inter.ILoginAView;
import com.sitanyun.merchant.guide.view.inter.ISmsLoginAView;
import com.sitanyun.merchant.guide.weiht.CodeEditView;
import com.sitanyun.merchant.guide.weiht.CountDownTimerUtils;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 验证码登录
 */
public class SmsLoginActivity extends BaseActivity implements ISmsLoginAView {

    @BindView(R.id.codeEditView)
    CodeEditView codeEditView;
    @BindView(R.id.sms_message)
    TextView smsMessage;
    @BindView(R.id.sms_time)
    TextView smsTime;
    @BindView(R.id.sms_finish)
    TextView smsFinish;
    @BindView(R.id.error_sms)
    TextView errorSms;
    private ISmsLoginAPresenter mISmsLoginAPresenter;
    private String phone;

    @Override
    public int getLayoutId() {
        return R.layout.activity_sms_login;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setFindViewById(true);
        setTitleStr("验证手机号码");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

    }

    @Override
    public void initView() {
        mISmsLoginAPresenter = new SmsLoginAPresenterImpl(this);
        phone = getIntent().getStringExtra("phone");
        smsMessage.setText("我们已经向" + phone + "发送验证码");
        CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(smsTime, 60000, 1000);
        mCountDownTimerUtils.start();
        if (!phone.isEmpty()) {
            mISmsLoginAPresenter.getsmslogin(getIntent().getStringExtra("phone"), "1");

        } else {
            ToastUtil.showToast(SmsLoginActivity.this, "无手机号");
        }
        codeEditView.setOnCompleteListener(new CodeEditView.OnCompleteListener() {
            @Override
            public void onComplete(String content) {
                mISmsLoginAPresenter.getLogin(phone, content);
            }
        });
//        codeEditView.setOnInputEndCallBack(new CodeEditView.inputEndListener() {
//            @Override
//            public void input(String text) { //输入完毕后的回调
//                mISmsLoginAPresenter.getLogin(phone, text);
//            }
//
//            @Override
//            public void afterTextChanged(String text) { //输入内容改变后的回调
//            }
//        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public <T> T request(int requestFlag) {
        return null;
    }

    @Override
    public <T> void response(T response, int responseFlag) {
        if (responseFlag == ILoginAView.RESPONSE_ONE) {
            SmsPhoneBean smsPhoneBean = (SmsPhoneBean) response;
            if (smsPhoneBean.getCode() == 0) {
                ToastUtil.showToast(SmsLoginActivity.this, "验证码发送成功");
            } else {
                ToastUtil.showToast(SmsLoginActivity.this, smsPhoneBean.getMsg() + "");
            }

        }
    }

    ///判断验证码登录
    @Override
    public <T> T requestlogin(int requestFlag) {
        return null;
    }

    @Override
    public <T> void responselogin(T response, int responseFlag) {
        if (responseFlag == ISmsLoginAView.RESPONSE_ONE) {
            LoginBean loginBean = (LoginBean) response;
            if (loginBean.getCode() == 0) {
                SharedPreferenceUtil.SaveData("phone", phone);
                SharedPreferenceUtil.SaveData("userid", loginBean.getUser_id());
                SharedPreferenceUtil.SaveData("token", loginBean.getAccess_token());
                Log.e("TTOKEN", loginBean.getAccess_token());

                SharedPreferenceUtil.SaveData("clientid", loginBean.getS_client_id());
                SharedPreferenceUtil.SaveData("nodeid", loginBean.getNode_id());
                SharedPreferenceUtil.SaveData("tonodeid", loginBean.getTop_node_id());
                //---------------------
                SharedPreferenceUtil.SaveData("tonodename", loginBean.getTop_node_name()+"");
                SharedPreferenceUtil.SaveData("sxtoken", loginBean.getRefresh_token());
                SharedPreferenceUtil.SaveData("staffid", loginBean.getStaff_id());
                SharedPreferenceUtil.SaveData("cid", loginBean.getS_client_id());
                errorSms.setVisibility(View.GONE);
                if (loginBean.isFirst_login()==true){
                    startActivity(new Intent(SmsLoginActivity.this, SetingPaswdActivity.class)
                    .putExtra("phone",phone).putExtra("username",loginBean.getUsername())
                    .putExtra("userId",loginBean.getUser_id()+""));
//                    finish();
                }else {
                    Intent intent = new Intent(SmsLoginActivity.this, MainActivity.class)
                            .putExtra("cid","null");
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                  finish();
//                 startActivity(new Intent(SmsLoginActivity.this, MainActivity.class));
//                 finish();
//                  startActivity(new Intent(SmsLoginActivity.this, SetingPaswdActivity.class)
//                           .putExtra("phone",phone).putExtra("username",loginBean.getUsername())
//                            .putExtra("userId",loginBean.getUser_id()+""));
                }

            } else {
                codeEditView.clearText();
                errorSms.setVisibility(View.VISIBLE);
                errorSms.setText(loginBean.getMsg());
            }

        }

    }

    @OnClick({R.id.sms_time, R.id.sms_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sms_time:
                if (smsTime.getText().equals("重新获取验证码")) {
                    mISmsLoginAPresenter.getsmslogin(getIntent().getStringExtra("phone"), "1");
                    CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(smsTime, 60000, 1000);
                    mCountDownTimerUtils.start();
                }
                break;
            case R.id.sms_finish:
                finish();
                break;
        }
    }
}
